<!--  -->
<template>
  <base-breadcrumb>
    <a-spin :spinning="spinning">
      <a-row :gutter="10">
        <a-col :span="24">
          <a-card class="requiredFormItem">
            <title-name title="基本信息" />
            <custom-form ref="formBase" :editModel="''" :is-validate="true" :rules="rules" :form="form" :form-list="formOption"></custom-form>
          </a-card>
        </a-col>
        <a-col :span="24">
          <component :key="componentName" :is="componentName" ref="component" :base-form-info="form" :statusBasicSuc="statusBasicSuc" :spinning.sync="spinning" @updateBasicDetail="updateBasicDetail" @closePage="closePage"></component>
        </a-col>
        <a-col :span="24">
          <a-card class="formFile">
            <title-name :title="pageName === 'PhasedLayoutPlan'?'分阶段平面布置图':'附件信息'" />
            <BTableUpload
              v-if="pageName === 'PhasedLayoutPlan'"
              v-model="form.fileList"
              :disabled="pageType === 'detail'"
              business-code="PLAN02"
            />
            <custom-form v-else ref="formBaseFile" :rules="rules" :formList="formFile" :form="form" :is-emit="false" :editModel="''"></custom-form>
          </a-card>
        </a-col>
      </a-row>
      <div class="tool-btns page-btn-right-top">
        <template v-if="pageType !== 'detail'">
          <a-button  v-if="pageType !== 'change'" class="ml20" :loading="loading" type="primary" @click="sumbit('SaveDrafts')"> 保存草稿 </a-button>
          <a-button class="ml20" :loading="loading" type="primary" @click="sumbit('Submit')"> 提交 </a-button>
        </template>
        <a-button class="ml20" type="primary" @click="$close()"> 关闭 </a-button>
      </div>
    </a-spin>
    <!-- 选择审批人 -->
      <selectApprover
        :business-id="businessId"
        :process-config="setProcessConfig"
      />
  </base-breadcrumb>
</template>

<script>
const allComponents = require.context('./components', false, /.*\.vue$/)
let res_components = {}
allComponents.keys().forEach((fileName) => {
  let comp = allComponents(fileName)
  res_components[fileName.replace(/^\.\/(.*)\.\w+$/, '$1')] = comp.default
})
import mixins from './mixins'
import { querySchemeBasicDetail,updateSchemeBasicDetail} from '@/api/project/plannManagement/index.js' // api
import moment from 'moment'

export default {
  name: 'plannTableComponents',
  mixins: [mixins],
  data() {
    return {
      loading: false,
      spinning: false,
      statusBasicSuc:false, //更新基本信息是否成功
      form: { fileList: [], fillName:null},
      rules: { basicInfo: [{ required: true, message: '请输入项目基本情况' }],
      fileList:[{ type: 'array', required: false, message: '请上传文件', trigger: 'blur' }]},
      pageType: '',
      pageName: this.$route.query.pageName,
      listUrl: '/cscec-project-business/schedule-plan-adjust/query-files',
      deleteUrl: '/cscec-project-business/schedule-plan-adjust/delete-files',
      uploadList: '/cscec-project-business/schedule-plan-adjust/add-files',
      businessId: null,
      processConfig: null,
      //点击提交发送流程配置值
      setProcessConfig: null,
    }
  },

  components: res_components,

  computed: {
    formOption() {
      return [
        {
          label: '所属单位',
          keyName: 'projectId',
          type: 'orgTree',
          disabled: true,
          span: 16
        },
        // { label: '所属分公司', keyName: 'branchComName', type: 'BaseInput', disabled: true },
        // { label: '项目名称', keyName: 'projectName', type: 'BaseInput', disabled: true },
        { label: '项目编号', keyName: 'projectCode', type: 'BaseInput', disabled: true },
        { label: '项目基本情况', keyName: 'basicInfo', type: 'BaseTextarea', span: 24, disabled: this.pageType === 'detail' ? true : false,attrs: {
            maxLength: 3000
          },},
        { label: '填报人', keyName: 'fillName', type: 'BaseInput', disabled: true },
        { label: '填报时间', keyName: 'fillTime', type: 'BaseInput', disabled: true }
      ]
    },
    formFile() {
      return [
        {
          keyName: 'fileList',
          type: 'uploadFile',
          label: '附件信息',
          span: 24,
          businessCode: 'PLAN02',
          disabled: this.pageType === 'detail' ? true : false,
         attrs: {
           delRequest:false,
           needChineseName:true
          },
        }
      ]
    },
    info() {
      return this.$route.query
    }
  },

  async created() {
    const { id, pageType, version = 0 } = this.$route.query
    this.businessId = id
    this.pageType = pageType
    this.querySchemeBasicDetail()
    this.processConfig = await this.$getConfigFn('scheme_process') //scheme_process
  },

  mounted() {
      this.$route.meta.title = [' 策划执行书明细表 查看', ' 策划执行书明细表 编辑'][this.info.editModel]
  },
  methods: {
    closePage() {
      this.spinning = false;
      if(this.pageType == 'change') {
        setTimeout(()=> {
          this.setProcessConfig = this.processConfig
        }, 100)
      }else {
        this.$multiTab.close();
      }
    },
    //更新基本信息
    updateBasicDetail(data) {
      this.setProcessConfig = null
      if(this.form.fileList.length < 1 && this.pageName === 'PhasedLayoutPlan') {
        return this.$message.warning('请传附件');
      }
      if(this.pageType == 'change') data.changed = true;
      updateSchemeBasicDetail(data).then((res)=>{
        this.statusBasicSuc = true
        if(this.pageName === 'PhasedLayoutPlan') {
          this.$message.success(data.status === 0 ? '保存成功' : '提交成功')
          this.$multiTab.close();
        }
      }).catch(() => {
           this.$message.warning(err.data.message)
        })
      },
    querySchemeBasicDetail() {
      const { id, version } = this.$route.query
      const query = {
        schemeDetailId: id
      }
      if (version) {
        query.latestData = false
        query.version = Number(version)
      } else {
        query.latestData = true
      }
      this.spinning = true
      querySchemeBasicDetail(query).then((res) => {
        this.spinning = false
        this.form = res.data
        this.form.fillName = res.data.fillName || JSON.parse(sessionStorage.getItem('ycloud-user_info')).name
        this.form.fillTime = res.data.fillTime || moment().format('YYYY-MM-DD')
        this.form.fileList = res.data.fileList?res.data.fileList:[]
      })
      .catch(() => {
        this.spinning = false
      })
      
    },
    sumbit(type = 'Submit') {
      if (type === 'Submit') {
        // let fileList = this.form.fileList;
        this.$refs.formBase.validate((valid) => {
          if (valid) {
            this.emitComponentSubmit(type)
            // if(!fileList || !fileList.length){
            //     this.$message.warning('请上传附件信息')
            //     return
            // }else{
            //   this.emitComponentSubmit(type)
            // }
          }else {
            this.$message.warning("请填写完所有必填项信息")
          }
        })
      } else {
        this.emitComponentSubmit(type)
      }
    },
  }
}
</script>
<style lang='less' scoped>
@deep: ~'>>>';
.formFile {
  @{deep} .ant-col-24 .ant-form-item {
    display: flex;
  }
  @{deep} .ant-form-item-label{
    width: 98px;
  }
  @{deep} .ant-form-item-label label::before{
  }
  @{deep} .file_prompt{
    display: none;
  }
}
</style>
